/*
 ============================================================================
 Name        : WebBrowserView.cpp
 Author      : 
 Version     :
 Copyright   : 
 Description : CWebBrowserView implementation
 ============================================================================
 */
#include <GDI.H>
#include "CutString.h"
#include "fFont.h"
#include "fGfxEngine.h"
#include "ApplicationScheme.h"
#include "YuebaoBuffer.h"
#include "BetterUserHBufC.h"
#include "ProjectsDefines.h"

//const TInt KGranularityOfArray = 10;
//const TInt KMaxLineCharNum = 32;
const TInt MAX_DIALOG_TEXT = 320;

///////////////////////////////////////////////////////////////////////////
//fCutString_
StringSpliteUtility::StringSpliteUtility( )
{
    iText = NULL;
    iTextArray = new ( ELeave ) CDesC16ArrayFlat( 5 );
}

StringSpliteUtility::~StringSpliteUtility( )
{
    SAFE_DELETE( iText );
    SAFE_DELETE( iTextArray );
}

#define char_is_lineend(ch)         ((ch) == '\r')
#define char_is_newline(ch)         ((ch) == '\n')
#define char_is_whitespace(ch)      ((ch) == ' ' || (ch) == '\t')
#define char_is_punctuation(ch)     ((ch) == ',' || (ch) == '.' || (ch) == '!' || (ch) == '\"')
//#define text_width(text)            iFont->TextWidthInPixels((text))+ iFont->TextWidthInPixels(_L("0"))



TBool StringSpliteUtility::CalcTextArray( )
{
    if( iText && iText->Length( ) != 0 && iWidth > 0 && iFont )
    {
        TBool hasBreak = EFalse;

        iTextArray->Reset( );

        YuebaoBuffer< MAX_DIALOG_TEXT > tempBuf;
        YuebaoBuffer< MAX_DIALOG_TEXT > findBreak;

        TPtr tempBufPtr = tempBuf.Des( );
        TPtr findBreakPtr = findBreak.Des( );

        TInt textLength = iText->Length( );
        TUint16 _char;
        TUint16 *ptr = ( TUint16 * )iText->Ptr( );

        //TBool isWordbreak = EFalse;
       // TBool isEngish = ETrue;

        for( TInt i = 0; i < textLength; i++ )
        {
            _char = *( ptr + i );

            if( char_is_lineend( _char ) )
            {
                continue;
            }

            if( char_is_newline( _char ) )
            {
                hasBreak = ETrue;
            }
            else if( char_is_whitespace( _char ) )
            {
                tempBufPtr.Append( ptr + i, 1 );
                TUint16 *isBreak = ( TUint16 * )iText->Ptr( );

                findBreakPtr.Copy( tempBufPtr );

                TUint16 _char2 = 0;
                for( TInt j = i + 1; j < textLength; j++ )
                {
                    findBreakPtr.Append( isBreak + j, 1 );
                    // this is a word break

                    _char2 = *( isBreak + j );
                    if( char_is_whitespace( _char2 ) || char_is_lineend( _char2 ) || char_is_newline( _char2 ) || _char2 >= 0xFF )
                    {
                        hasBreak = EFalse;
                        break;
                    }

                    // text width large than the rect width
                    if( text_width( findBreakPtr ) >= iWidth )
                    {
                        hasBreak = ETrue;
                        break;
                    }
                }
            }
            else
            {
                tempBufPtr.Append( ptr + i, 1 );
            }

            if( hasBreak || text_width( tempBufPtr ) >= iWidth - 4 )
            {
                HBufC *temp = tempBufPtr.Alloc( );

                iTextArray->AppendL( temp->Des( ) );
                delete temp;

                tempBufPtr.Zero( );
                hasBreak = EFalse;
            }
        }

        if( tempBufPtr.Length( ) > 0 )
        {
            HBufC *temp = tempBufPtr.Alloc( );

            iTextArray->AppendL( temp->Des( ) );
            delete temp;
        }

        return ETrue;
    }

    return EFalse;
}

_LIT( kZero, "0" );

void StringSpliteUtility::SetFont( const CFont * aFont )
{
    iFont = aFont;
    _zeroWidth = iFont->TextWidthInPixels( kZero );
    iTextArray->Reset( );
}

void StringSpliteUtility::SetText( const TDesC & aText )
{
    BetterUtility::Copy( iText, aText );
    iTextArray->Reset( );
}

void StringSpliteUtility::SetWidth( TInt aWidth )
{
    iWidth = aWidth;
    iTextArray->Reset( );
}

CDesC16Array & StringSpliteUtility::GetTextArray( )
{
    if( iTextArray->Count( ) == 0 )
    {
        CalcTextArray( );
    }

    return *iTextArray;
}
